package com.musiklegal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursorDriver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQuery;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class DataHelper {
   private static final String DATABASE_NAME = "license.db";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_NAME = "license";
   private Context context;
   private SQLiteDatabase db;
   private SQLiteStatement insertStmt;
   private static final String INSERT = "insert into "
      + TABLE_NAME + "(idring,playcount) values (?,?)";
   
   public DataHelper(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
//      db = SQLiteDatabase.openOrCreateDatabase(MusikLegal.licenceFile,null);
      db = openHelper.getWritableDatabase();
      this.insertStmt = this.db.compileStatement(INSERT);
   }
   
   public boolean isOpen()
   {
	   return db.isOpen();
   }
   
   public void closeDB()
   {
	  
	   db.close();
   }
   
   boolean substract(String ID)
   {
	   int i=getData(ID).play;
	   if(i>0)
	   {
		   i--;
		   update(ID,i);
		   return true;
	   }
	   return false;
   }
   
   boolean checkSong(String ID)
   {
	  License p = getData(ID);
	  if(p!=null&&p.play>0)
		  return true;
	  else return false;
	   
   }
   
//   private License getSong(String id)
//   {
//	   Cursor c = db.query(TABLE_NAME, new String[]{"idring","playcount"}, "idring='"+id+"'", null, null, null, null);
//	   
//	   return ;
//   }
   
   public long insert(String name,int t) 
   {
	   try{
	      this.insertStmt.bindString(1, name);
	      this.insertStmt.bindLong(2, t);
	      return this.insertStmt.executeInsert();
	   }
	   catch(Exception e)
	   {
		   return -1;
	   }
   }
   
    int getCount(String id)
   {
	   License p = getData(id);
	   if(p!=null)
	   return p.play;
	   else return -1;
   }
   
   License getData(String id)
   {
	   if(searchId(id))
	   {
		   License l = null;
		   Cursor cursor = this.db.query(TABLE_NAME, new String[] { "idring","playcount" },
				   "idring='"+id+"'" , null, null, null, null);
		   if (cursor.moveToFirst()) {
		         
		        	  l = new License(cursor.getString(0),Integer.parseInt(cursor.getString(1)));
		        	  
		        
		      }
		      if (cursor != null && !cursor.isClosed()) {
		         cursor.close();
		      }
		      //cursor.close();
		      
		   return l;
	   }
	   else return null;
   }
   
   public boolean update(String id,int play)
   {
	   try{
		   ContentValues val = new ContentValues();
		   val.put("playcount", play);
		   db.update(TABLE_NAME, val, "idring='"+id+"'", null);
		   return true;
	   }
	   catch(Exception e)
	   {
		   return false;
	   }
   }
   
   public boolean searchId(String ID)
   {
	   
	   Cursor c = db.query(TABLE_NAME, new String[]{"idring"}, "idring='"+ID+"'", null, null, null, null);
	   boolean t =c.moveToNext();
	   c.close();
	   return t;
   }
   
   public void deleteAll() {
      this.db.delete(TABLE_NAME, null, null);
   }
   public List<License> selectAll() {
      List<License> list = new ArrayList<License>();
      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "idring","playcount" },
        null, null, null, null, null);
      if (cursor.moveToFirst()) {
         do {
        	 License l = new License(cursor.getString(0),cursor.getShort(1));
             list.add(l);
         } while (cursor.moveToNext());
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      return list;
   }
   private static class OpenHelper extends SQLiteOpenHelper {
      OpenHelper(Context context) {
         super(context, MusikLegal.licenceFile, null, DATABASE_VERSION);
      }
      @Override
      public void onCreate(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE " + TABLE_NAME + "(idring VARCHAR(12) PRIMARY KEY, playcount TEXT)");
      }
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
      }
   }
   
   static class License
   {
	   String idring;
	   int play;
	   License(String id,int count)
	   {
		   idring = id;
		   play=count;
	   }
	   public String toString()
	   {
		   return idring+" "+play;
	   }
   }
}